package com.open.exam.utils;

import com.openexam.openqti.openqtipaperquestion.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXBContext;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author devsun
 *         Created by on 2017/11/17.
 */
public class LoadXml {

    private static Logger logger = LoggerFactory.getLogger(LoadXml.class);

    public static void main(String[] args) throws Exception {
        String path = LoadXml.class.getResource("/").getPath().replaceFirst("/", "");
//        String filePath = path.substring(0,path.indexOf("build")) + "src/main/examFile/examdata/yq2017/AST_0fc28276-9b81-e711-b5da-0050568c7d4e.xml";
        LoadXml loadXml = new LoadXml();
//        qitTest.getSize();
//        filePath = path.substring(0,path.indexOf("build")) + "src/main/examFile/examdata/yq2017/AssessmentItems/ASI_92a113ee-e2e9-49b6-8e5b-de0181f7b6a5.xml";
//
//        qitTest.loadExamPlanXml(filePath);
//        qitTest.loadExamPaperXml(filePath);
//        qitTest.loadQuestionXml(filePath);
        String filePath = path.substring(0,path.indexOf("build")) + "src/main/examFile/examdata/yq2017/AssessmentItems/aca54b1b-abc4-419a-a5cc-59b4b5ae2109/2016928172540402.jpg";
//        loadXml.copyFile(filePath,path.substring(0,path.indexOf("build")) + "src/main/resources/static/files/2016928172540402.jpg");
//        String url = "aca54b1b-abc4-419a-a5cc-59b4b5ae2109/2016928172540402.jpg";
//        yq2017/AssessmentItems/aca54b1b-abc4-419a-a5cc-59b4b5ae2109/2016928172540402.jpg
        String assmentFolder = "/AssessmentItems/";
        String imgUrl = "cb6c5f59-5174-4736-82ff-b60e4e759a1b/2016928165948608.jpg";
        String basePath = path.substring(0,path.indexOf("build")) + "src/main/";
        String imagePath = "/resources/static/files/";
        filePath = basePath + "examFile/examdata/yq2017" + assmentFolder + imgUrl;
        String examFile = filePath.substring(0,filePath.indexOf("examFile"));
        FileUtil.copyImage(basePath,imagePath,filePath,imgUrl);
        //判断一个文件夹是否存在
//        File file = new File(filePath + split[0]);
//        if(!file.exists()){
//            file.mkdirs();
//            loadXml.copyFile(path.substring(0,path.indexOf("build")) + "src/main/examFile/examdata/yq2017/AssessmentItems/aca54b1b-abc4-419a-a5cc-59b4b5ae2109/2016928172540402.jpg",filePath+url);
//        }
    }

//    public void test(String path){
//        URL resource = getClass().getResource(path);
//        LoadXml loadXml = new LoadXml();
//        loadXml.copyFile(path.substring(0,path.indexOf("build")) + "src/main/examFile/examdata/yq2017/AssessmentItems/aca54b1b-abc4-419a-a5cc-59b4b5ae2109/2016928172540402.jpg",resource.getPath());
//    }


    /**
     * 读取试卷xml
     * @param xmlPath
     * @return
     * @throws Exception
     */
    public OpenAssessmentTest loadExamPaperXml(String xmlPath, JAXBContext jc) throws Exception {
        String examPaperXml = FileUtil.getFileContent(xmlPath);
        OpenAssessmentTest openAssessmentTest = new OpenAssessmentTest();
        openAssessmentTest.LoadFromString(examPaperXml,jc);
        return openAssessmentTest;
    }

    public OpenAssessmentItem loadQuestionXml(String xmlPath, ItemType itemType, JAXBContext jc) throws Exception {
        String fileContent = FileUtil.getFileContent(xmlPath);
        OpenAssessmentItem item = OpenAssessmentItem.createInstance(itemType);
        if(fileContent.isEmpty()){
            logger.info("试题内容为空，试题路径为：" + xmlPath);
        }
        item.LoadFromString(fileContent,jc);
        return item;
    }


    public List<OpenAssessmentItem> getQuestionList(OpenAssessmentTest openAssessmentTest, String filePath, JAXBContext jc) throws Exception {
        List<OpenAssessmentItem> openAssessmentItemList = new ArrayList<>();
        //遍历试卷中的试题信息
        if(openAssessmentTest != null){
            for (OpenTestPart openTestPart: openAssessmentTest.getTestPartCollection()) {
                for (OpenAssessmentSection openAssessmentSection: openTestPart.getAssessmentSectionCollection()) {
                    for (OpenAssessmentItemRef openAssessmentItemRef: openAssessmentSection.getAssessmentItemRefCollection()) {
                        //获取试题
                        String questionFilePath = filePath + "/" + openAssessmentItemRef.getHref();
                        OpenAssessmentItem item = loadQuestionXml(questionFilePath,openAssessmentItemRef.getType(),jc);

                        if(item == null){
                            throw new Exception("从考试计划包读取指定试题信息失败!");
                        }
                        openAssessmentItemList.add(item);
                    }
                }
            }
        }
        return openAssessmentItemList;
    }

    /**
     * 获取试卷中小题的数量
     * @param openAssessmentTest
     * @return
     */
    public static Integer getPaperQuestionCount(OpenAssessmentTest openAssessmentTest){
        Integer itemCount = 0;
        for (OpenTestPart openTestPart : openAssessmentTest.getTestPartCollection()) {
            for (OpenAssessmentSection section:openTestPart.getAssessmentSectionCollection()) {
                itemCount += section.getAssessmentItemRefCollection().size();
            }
        }
        return itemCount;
    }

    /**
     * 提取试题中包含的所有附件路径
     * @param itemContentXml 试题内容XML
     * @return
     */
    public List<String> findAttachmentPathList(String itemContentXml){
        List<String> pathList = new ArrayList<>();
        Pattern compile = Pattern.compile("(?<=(href|src)=)(?<Q>\"|')(?<PATH>.*?)\\k<Q>");
        Matcher matcher = compile.matcher(itemContentXml);
        while (matcher.find()){
            String path = matcher.group("PATH");
            path = path.replace("\\","/");
            pathList.add(path);
        }
        return pathList;
    }

}
